package b2c

import (
	"jlpay-sdk-go/pkg/jlpay/core"
	"jlpay-sdk-go/pkg/jlpay/models"
	"jlpay-sdk-go/pkg/jlpay/models/trans/dto"
)

// MicroPayRequest 微支付请求
//
// @author liaochangyi
// @since 2025-01-25
type MicroPayRequest struct {
	models.OrgBaseRequest
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// PaymentValidTime 订单有效期
	PaymentValidTime string `json:"payment_valid_time,omitempty"`
	// Body 商品标题
	Body string `json:"body,omitempty"`
	// Attach 商品描述
	Attach string `json:"attach,omitempty"`
	// TotalFee 交易金额
	TotalFee string `json:"total_fee,omitempty"`
	// AuthCode 用户付款码
	AuthCode string `json:"auth_code,omitempty"`
	// LimitPay 指定支付方式
	LimitPay string `json:"limit_pay,omitempty"`
	// IsHirePurchase 分期标识
	IsHirePurchase string `json:"is_hire_purchase,omitempty"`
	// HirePurchaseNum 分期标识
	HirePurchaseNum string `json:"hire_purchase_num,omitempty"`
	// HirePurchaseSellerPercent 卖家承担的手续费比例
	HirePurchaseSellerPercent string `json:"hire_purchase_seller_percent,omitempty"`
	// Remark 备注
	Remark string `json:"remark,omitempty"`
	// OpUserID 操作员
	OpUserID string `json:"op_user_id,omitempty"`
	// OpShopID 门店号
	OpShopID string `json:"op_shop_id,omitempty"`
	// DeviceInfo 终端设备号
	DeviceInfo string `json:"device_info,omitempty"`
	// MchCreateIP 商户IP
	MchCreateIP string `json:"mch_create_ip,omitempty"`
	// Longitude 经度
	Longitude string `json:"longitude,omitempty"`
	// Latitude 纬度
	Latitude string `json:"latitude,omitempty"`
	// SubAppid 子商户appid
	SubAppid string `json:"sub_appid,omitempty"`
	// GoodsTag 商品标记
	GoodsTag string `json:"goods_tag,omitempty"`
	// GoodsData 商品详情
	GoodsData *dto.GoodsDataDto `json:"goods_data,omitempty"`
	// TerminalInfo 终端信息
	TerminalInfo *dto.TerminalInfoDto `json:"terminal_info,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewMicroPayRequest 创建微支付请求
func NewMicroPayRequest() *MicroPayRequest {
	return &MicroPayRequest{}
}

// SetMchID 设置商户号
func (r *MicroPayRequest) SetMchID(mchID string) *MicroPayRequest {
	r.MchID = mchID
	return r
}

// SetTermNo 设置终端号
func (r *MicroPayRequest) SetTermNo(termNo string) *MicroPayRequest {
	r.TermNo = termNo
	return r
}

// SetPayType 设置交易类型
func (r *MicroPayRequest) SetPayType(payType string) *MicroPayRequest {
	r.PayType = payType
	return r
}

// SetOutTradeNo 设置外部订单号
func (r *MicroPayRequest) SetOutTradeNo(outTradeNo string) *MicroPayRequest {
	r.OutTradeNo = outTradeNo
	return r
}

// SetPaymentValidTime 设置订单有效期
func (r *MicroPayRequest) SetPaymentValidTime(paymentValidTime string) *MicroPayRequest {
	r.PaymentValidTime = paymentValidTime
	return r
}

// SetBody 设置商品标题
func (r *MicroPayRequest) SetBody(body string) *MicroPayRequest {
	r.Body = body
	return r
}

// SetAttach 设置商品描述
func (r *MicroPayRequest) SetAttach(attach string) *MicroPayRequest {
	r.Attach = attach
	return r
}

// SetTotalFee 设置交易金额
func (r *MicroPayRequest) SetTotalFee(totalFee string) *MicroPayRequest {
	r.TotalFee = totalFee
	return r
}

// SetAuthCode 设置用户付款码
func (r *MicroPayRequest) SetAuthCode(authCode string) *MicroPayRequest {
	r.AuthCode = authCode
	return r
}

// SetLimitPay 设置指定支付方式
func (r *MicroPayRequest) SetLimitPay(limitPay string) *MicroPayRequest {
	r.LimitPay = limitPay
	return r
}

// SetIsHirePurchase 设置分期标识
func (r *MicroPayRequest) SetIsHirePurchase(isHirePurchase string) *MicroPayRequest {
	r.IsHirePurchase = isHirePurchase
	return r
}

// SetHirePurchaseNum 设置分期标识
func (r *MicroPayRequest) SetHirePurchaseNum(hirePurchaseNum string) *MicroPayRequest {
	r.HirePurchaseNum = hirePurchaseNum
	return r
}

// SetHirePurchaseSellerPercent 设置卖家承担的手续费比例
func (r *MicroPayRequest) SetHirePurchaseSellerPercent(hirePurchaseSellerPercent string) *MicroPayRequest {
	r.HirePurchaseSellerPercent = hirePurchaseSellerPercent
	return r
}

// SetRemark 设置备注
func (r *MicroPayRequest) SetRemark(remark string) *MicroPayRequest {
	r.Remark = remark
	return r
}

// SetOpUserID 设置操作员
func (r *MicroPayRequest) SetOpUserID(opUserID string) *MicroPayRequest {
	r.OpUserID = opUserID
	return r
}

// SetOpShopID 设置门店号
func (r *MicroPayRequest) SetOpShopID(opShopID string) *MicroPayRequest {
	r.OpShopID = opShopID
	return r
}

// SetDeviceInfo 设置终端设备号
func (r *MicroPayRequest) SetDeviceInfo(deviceInfo string) *MicroPayRequest {
	r.DeviceInfo = deviceInfo
	return r
}

// SetMchCreateIP 设置商户IP
func (r *MicroPayRequest) SetMchCreateIP(mchCreateIP string) *MicroPayRequest {
	r.MchCreateIP = mchCreateIP
	return r
}

// SetLongitude 设置经度
func (r *MicroPayRequest) SetLongitude(longitude string) *MicroPayRequest {
	r.Longitude = longitude
	return r
}

// SetLatitude 设置纬度
func (r *MicroPayRequest) SetLatitude(latitude string) *MicroPayRequest {
	r.Latitude = latitude
	return r
}

// SetSubAppid 设置子商户appid
func (r *MicroPayRequest) SetSubAppid(subAppid string) *MicroPayRequest {
	r.SubAppid = subAppid
	return r
}

// SetGoodsTag 设置商品标记
func (r *MicroPayRequest) SetGoodsTag(goodsTag string) *MicroPayRequest {
	r.GoodsTag = goodsTag
	return r
}

// SetGoodsData 设置商品详情
func (r *MicroPayRequest) SetGoodsData(goodsData *dto.GoodsDataDto) *MicroPayRequest {
	r.GoodsData = goodsData
	return r
}

// SetTerminalInfo 设置终端信息
func (r *MicroPayRequest) SetTerminalInfo(terminalInfo *dto.TerminalInfoDto) *MicroPayRequest {
	r.TerminalInfo = terminalInfo
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *MicroPayRequest) SetRawRequestMap(rawMap map[string]interface{}) *MicroPayRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *MicroPayRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *MicroPayRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.MchID != "" {
		baseMap["mch_id"] = r.MchID
	}
	if r.TermNo != "" {
		baseMap["term_no"] = r.TermNo
	}
	if r.PayType != "" {
		baseMap["pay_type"] = r.PayType
	}
	if r.OutTradeNo != "" {
		baseMap["out_trade_no"] = r.OutTradeNo
	}
	if r.PaymentValidTime != "" {
		baseMap["payment_valid_time"] = r.PaymentValidTime
	}
	if r.Body != "" {
		baseMap["body"] = r.Body
	}
	if r.Attach != "" {
		baseMap["attach"] = r.Attach
	}
	if r.TotalFee != "" {
		baseMap["total_fee"] = r.TotalFee
	}
	if r.AuthCode != "" {
		baseMap["auth_code"] = r.AuthCode
	}
	if r.LimitPay != "" {
		baseMap["limit_pay"] = r.LimitPay
	}
	if r.IsHirePurchase != "" {
		baseMap["is_hire_purchase"] = r.IsHirePurchase
	}
	if r.HirePurchaseNum != "" {
		baseMap["hire_purchase_num"] = r.HirePurchaseNum
	}
	if r.HirePurchaseSellerPercent != "" {
		baseMap["hire_purchase_seller_percent"] = r.HirePurchaseSellerPercent
	}
	if r.Remark != "" {
		baseMap["remark"] = r.Remark
	}
	if r.OpUserID != "" {
		baseMap["op_user_id"] = r.OpUserID
	}
	if r.OpShopID != "" {
		baseMap["op_shop_id"] = r.OpShopID
	}
	if r.DeviceInfo != "" {
		baseMap["device_info"] = r.DeviceInfo
	}
	if r.MchCreateIP != "" {
		baseMap["mch_create_ip"] = r.MchCreateIP
	}
	if r.Longitude != "" {
		baseMap["longitude"] = r.Longitude
	}
	if r.Latitude != "" {
		baseMap["latitude"] = r.Latitude
	}
	if r.SubAppid != "" {
		baseMap["sub_appid"] = r.SubAppid
	}
	if r.GoodsTag != "" {
		baseMap["goods_tag"] = r.GoodsTag
	}

	// 添加复杂对象
	if r.GoodsData != nil {
		baseMap["goods_data"] = r.GoodsData
	}

	if r.TerminalInfo != nil {
		baseMap["terminal_info"] = r.TerminalInfo
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *MicroPayRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// ==================== 微支付响应 ====================

// MicroPayResponse 微支付响应
type MicroPayResponse struct {
	models.StandardResponse
	// Status 状态 1：待确认2：成功3：失败
	Status string `json:"status,omitempty"`
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
	// DeviceInfo 终端设备号
	DeviceInfo string `json:"device_info,omitempty"`
	// TransactionID 平台订单号
	TransactionID string `json:"transaction_id,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// ChnTransactionID 渠道订单号
	ChnTransactionID string `json:"chn_transaction_id,omitempty"`
	// TotalFee 交易金额
	TotalFee interface{} `json:"total_fee,omitempty"`
	// OrderTime 订单时间
	OrderTime string `json:"order_time,omitempty"`
	// TransTime 交易时间
	TransTime string `json:"trans_time,omitempty"`
	// FinnalAmount 实际付款金额
	FinnalAmount interface{} `json:"finnal_amount,omitempty"`
	// DiscountAmount 优惠总金额
	DiscountAmount interface{} `json:"discount_amount,omitempty"`
	// DiscountName 优惠活动名称
	DiscountName string `json:"discount_name,omitempty"`
	// CouponInfo 优惠详情
	CouponInfo interface{} `json:"coupon_info,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// SubOpenid 用户openid
	SubOpenid string `json:"sub_openid,omitempty"`
}

// NewMicroPayResponse 创建微支付响应
func NewMicroPayResponse() *MicroPayResponse {
	return &MicroPayResponse{}
}

// ==================== 微支付预授权请求 ====================

// MicroPayPreAuthRequest 微支付预授权请求
type MicroPayPreAuthRequest struct {
	models.OrgBaseRequest
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// PaymentValidTime 订单有效期
	PaymentValidTime string `json:"payment_valid_time,omitempty"`
	// GuaranteeExpiry 担保有效期
	GuaranteeExpiry string `json:"guarantee_expiry,omitempty"`
	// Body 商品标题
	Body string `json:"body,omitempty"`
	// Attach 商品描述
	Attach string `json:"attach,omitempty"`
	// TotalFee 交易金额
	TotalFee string `json:"total_fee,omitempty"`
	// AuthCode 用户付款码
	AuthCode string `json:"auth_code,omitempty"`
	// Remark 备注
	Remark string `json:"remark,omitempty"`
	// OpUserID 操作员
	OpUserID string `json:"op_user_id,omitempty"`
	// OpShopID 门店号
	OpShopID string `json:"op_shop_id,omitempty"`
	// DeviceInfo 终端设备号
	DeviceInfo string `json:"device_info,omitempty"`
	// MchCreateIP 商户IP
	MchCreateIP string `json:"mch_create_ip,omitempty"`
	// TransAddress 交易地址
	TransAddress string `json:"trans_address,omitempty"`
	// AreaCode 地区代码
	AreaCode string `json:"area_code,omitempty"`
	// Longitude 经度
	Longitude string `json:"longitude,omitempty"`
	// Latitude 纬度
	Latitude string `json:"latitude,omitempty"`
	// SyncAble 同步标识
	SyncAble bool `json:"sync_able,omitempty"`
	// SubAppid 子商户appid
	SubAppid string `json:"sub_appid,omitempty"`
	// TerminalInfo 终端信息
	TerminalInfo *dto.TerminalInfoDto `json:"terminal_info,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewMicroPayPreAuthRequest 创建微支付预授权请求
func NewMicroPayPreAuthRequest() *MicroPayPreAuthRequest {
	return &MicroPayPreAuthRequest{}
}

// SetMchID 设置商户号
func (r *MicroPayPreAuthRequest) SetMchID(mchID string) *MicroPayPreAuthRequest {
	r.MchID = mchID
	return r
}

// SetTermNo 设置终端号
func (r *MicroPayPreAuthRequest) SetTermNo(termNo string) *MicroPayPreAuthRequest {
	r.TermNo = termNo
	return r
}

// SetPayType 设置交易类型
func (r *MicroPayPreAuthRequest) SetPayType(payType string) *MicroPayPreAuthRequest {
	r.PayType = payType
	return r
}

// SetOutTradeNo 设置外部订单号
func (r *MicroPayPreAuthRequest) SetOutTradeNo(outTradeNo string) *MicroPayPreAuthRequest {
	r.OutTradeNo = outTradeNo
	return r
}

// SetPaymentValidTime 设置订单有效期
func (r *MicroPayPreAuthRequest) SetPaymentValidTime(paymentValidTime string) *MicroPayPreAuthRequest {
	r.PaymentValidTime = paymentValidTime
	return r
}

// SetGuaranteeExpiry 设置担保有效期
func (r *MicroPayPreAuthRequest) SetGuaranteeExpiry(guaranteeExpiry string) *MicroPayPreAuthRequest {
	r.GuaranteeExpiry = guaranteeExpiry
	return r
}

// SetBody 设置商品标题
func (r *MicroPayPreAuthRequest) SetBody(body string) *MicroPayPreAuthRequest {
	r.Body = body
	return r
}

// SetAttach 设置商品描述
func (r *MicroPayPreAuthRequest) SetAttach(attach string) *MicroPayPreAuthRequest {
	r.Attach = attach
	return r
}

// SetTotalFee 设置交易金额
func (r *MicroPayPreAuthRequest) SetTotalFee(totalFee string) *MicroPayPreAuthRequest {
	r.TotalFee = totalFee
	return r
}

// SetAuthCode 设置用户付款码
func (r *MicroPayPreAuthRequest) SetAuthCode(authCode string) *MicroPayPreAuthRequest {
	r.AuthCode = authCode
	return r
}

// SetRemark 设置备注
func (r *MicroPayPreAuthRequest) SetRemark(remark string) *MicroPayPreAuthRequest {
	r.Remark = remark
	return r
}

// SetOpUserID 设置操作员
func (r *MicroPayPreAuthRequest) SetOpUserID(opUserID string) *MicroPayPreAuthRequest {
	r.OpUserID = opUserID
	return r
}

// SetOpShopID 设置门店号
func (r *MicroPayPreAuthRequest) SetOpShopID(opShopID string) *MicroPayPreAuthRequest {
	r.OpShopID = opShopID
	return r
}

// SetDeviceInfo 设置终端设备号
func (r *MicroPayPreAuthRequest) SetDeviceInfo(deviceInfo string) *MicroPayPreAuthRequest {
	r.DeviceInfo = deviceInfo
	return r
}

// SetMchCreateIP 设置商户IP
func (r *MicroPayPreAuthRequest) SetMchCreateIP(mchCreateIP string) *MicroPayPreAuthRequest {
	r.MchCreateIP = mchCreateIP
	return r
}

// SetTransAddress 设置交易地址
func (r *MicroPayPreAuthRequest) SetTransAddress(transAddress string) *MicroPayPreAuthRequest {
	r.TransAddress = transAddress
	return r
}

// SetAreaCode 设置地区代码
func (r *MicroPayPreAuthRequest) SetAreaCode(areaCode string) *MicroPayPreAuthRequest {
	r.AreaCode = areaCode
	return r
}

// SetLongitude 设置经度
func (r *MicroPayPreAuthRequest) SetLongitude(longitude string) *MicroPayPreAuthRequest {
	r.Longitude = longitude
	return r
}

// SetLatitude 设置纬度
func (r *MicroPayPreAuthRequest) SetLatitude(latitude string) *MicroPayPreAuthRequest {
	r.Latitude = latitude
	return r
}

// SetSyncAble 设置同步标识
func (r *MicroPayPreAuthRequest) SetSyncAble(syncAble bool) *MicroPayPreAuthRequest {
	r.SyncAble = syncAble
	return r
}

// SetSubAppid 设置子商户appid
func (r *MicroPayPreAuthRequest) SetSubAppid(subAppid string) *MicroPayPreAuthRequest {
	r.SubAppid = subAppid
	return r
}

// SetTerminalInfo 设置终端信息
func (r *MicroPayPreAuthRequest) SetTerminalInfo(terminalInfo *dto.TerminalInfoDto) *MicroPayPreAuthRequest {
	r.TerminalInfo = terminalInfo
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *MicroPayPreAuthRequest) SetRawRequestMap(rawMap map[string]interface{}) *MicroPayPreAuthRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *MicroPayPreAuthRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *MicroPayPreAuthRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.MchID != "" {
		baseMap["mch_id"] = r.MchID
	}
	if r.TermNo != "" {
		baseMap["term_no"] = r.TermNo
	}
	if r.PayType != "" {
		baseMap["pay_type"] = r.PayType
	}
	if r.OutTradeNo != "" {
		baseMap["out_trade_no"] = r.OutTradeNo
	}
	if r.PaymentValidTime != "" {
		baseMap["payment_valid_time"] = r.PaymentValidTime
	}
	if r.GuaranteeExpiry != "" {
		baseMap["guarantee_expiry"] = r.GuaranteeExpiry
	}
	if r.Body != "" {
		baseMap["body"] = r.Body
	}
	if r.Attach != "" {
		baseMap["attach"] = r.Attach
	}
	if r.TotalFee != "" {
		baseMap["total_fee"] = r.TotalFee
	}
	if r.AuthCode != "" {
		baseMap["auth_code"] = r.AuthCode
	}
	if r.Remark != "" {
		baseMap["remark"] = r.Remark
	}
	if r.OpUserID != "" {
		baseMap["op_user_id"] = r.OpUserID
	}
	if r.OpShopID != "" {
		baseMap["op_shop_id"] = r.OpShopID
	}
	if r.DeviceInfo != "" {
		baseMap["device_info"] = r.DeviceInfo
	}
	if r.MchCreateIP != "" {
		baseMap["mch_create_ip"] = r.MchCreateIP
	}
	if r.TransAddress != "" {
		baseMap["trans_address"] = r.TransAddress
	}
	if r.AreaCode != "" {
		baseMap["area_code"] = r.AreaCode
	}
	if r.Longitude != "" {
		baseMap["longitude"] = r.Longitude
	}
	if r.Latitude != "" {
		baseMap["latitude"] = r.Latitude
	}
	baseMap["sync_able"] = r.SyncAble
	if r.SubAppid != "" {
		baseMap["sub_appid"] = r.SubAppid
	}

	// 添加复杂对象
	if r.TerminalInfo != nil {
		baseMap["terminal_info"] = r.TerminalInfo
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *MicroPayPreAuthRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// ==================== 微支付预授权响应 ====================

// MicroPayPreAuthResponse 微支付预授权响应
type MicroPayPreAuthResponse struct {
	models.StandardResponse
	// Status 状态 1：待确认2：成功3：失败
	Status string `json:"status,omitempty"`
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
	// DeviceInfo 终端设备号
	DeviceInfo string `json:"device_info,omitempty"`
	// TransactionID 平台订单号
	TransactionID string `json:"transaction_id,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// ChnTransactionID 渠道订单号
	ChnTransactionID string `json:"chn_transaction_id,omitempty"`
	// TotalFee 交易金额
	TotalFee interface{} `json:"total_fee,omitempty"`
	// OrderTime 订单时间
	OrderTime string `json:"order_time,omitempty"`
	// TransTime 交易时间
	TransTime string `json:"trans_time,omitempty"`
	// FinnalAmount 实际付款金额
	FinnalAmount interface{} `json:"finnal_amount,omitempty"`
	// DiscountAmount 优惠总金额
	DiscountAmount interface{} `json:"discount_amount,omitempty"`
	// DiscountName 优惠活动名称
	DiscountName string `json:"discount_name,omitempty"`
	// CouponInfo 优惠详情
	CouponInfo interface{} `json:"coupon_info,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// SubOpenid 用户openid
	SubOpenid string `json:"sub_openid,omitempty"`
}

// NewMicroPayPreAuthResponse 创建微支付预授权响应
func NewMicroPayPreAuthResponse() *MicroPayPreAuthResponse {
	return &MicroPayPreAuthResponse{}
}
